package com.lulin.mall.product.thread;

import java.util.concurrent.*;

/**
 * @author lulin
 * @date 2022/11/30 17:47
 */
public class threadDemo {
    //定义1个线程池对象
    private static ExecutorService threadPool = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main方法——执行了...");
        threadDemo1 t1 = new threadDemo1();
        t1.start();

        threadDemo2 t2 = new threadDemo2();
        Thread thread = new Thread(t2);
        thread.start();
/*        //拉姆达表达式
        new Thread(() -> {
            System.out.println("表达式——" + Thread.currentThread().getName());
        }).start();*/

        //通过Callable接口来实现  FutureTask本值上是一个Runnable接口
        MyCallable myCallable = new MyCallable();
        FutureTask futureTask = new FutureTask(myCallable);
        Thread t3 = new Thread(futureTask);
        t3.start();

        // 阻塞,等待子线程的执行完成。获取线程的返回结果
        Object o = futureTask.get();
        System.out.println("o = " + o);

        //线程池的相关处理
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程池——当前线程：" + Thread.currentThread().getName());
            }
        });

        System.out.println("main方法——结束了...");

    }
}

//第一种
class threadDemo1 extends Thread {
    @Override
    public void run() {
        System.out.println("继承Thread方式——当前线程：" + Thread.currentThread().getName());
    }
}

//第二种
class threadDemo2 implements Runnable {

    @Override
    public void run() {
        System.out.println("实现Runnable接口方式——当前线程：" + Thread.currentThread().getName());
    }
}

//第三种
class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("实现Callable方式——当前线程：" + Thread.currentThread().getName());
        return 10;
    }
}

